package cn.dt.ymcc.mqconsumer;

import cn.dt.ymcc.domain.PayOrder;
import cn.dt.ymcc.dto.PayDto;
import cn.dt.ymcc.service.IPayOrderService;
import cn.dt.ymcc.util.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@RocketMQMessageListener(
        consumerGroup = "order-pay-consumer",
        topic = "order-topic",
        selectorExpression = "order-args"
)
public class PayDelayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;
    /**
     * 将生产者存入message的信息（t_pay_order需要存入的数据）payDto拿出来，先判断是否能拿到，拿不到就报错
     * 1.拿到以后先根据订单号查询t_pay_order中是否存在，如果存在则放置，说明已经消费
     * 2.拿不到则保存数据
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        AssertUtil.isNotNull(body,"消息没有拿到");
        String orderNo = new String(body, StandardCharsets.UTF_8);
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        AssertUtil.isNotNull(payOrder,"不存在支付单，订单号为空");
        payOrder.setPayStatus(4);
        payOrderService.updateById(payOrder);


    }
}
